"use strict";

//var dataUrl = /^\s*data:([a-z]+\/[a-z]+(;[a-z\-]+\=[a-z\-]+)?)?(;base64)?,[a-z0-9\!\$\&\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i;


app.directive("ngSrcCache", ["imageStore", function (imageStore) {
    var dataUrl = /^\s*data:image/;

    return {
        priority: 99,
        link: function (scope, element, attr) {
            //element.addClass("preloadable");
            //element.addClass("empty");
            var propName = "src",
            name = "src";

            attr.$observe("ngSrcCache", function (value) {
                if (!value) return;
                if (value.match(dataUrl)) {
                    //console.log("set src as dataUri");
                    attr.$set(name, value);
                    //picturefill({ reevaluate: true });
                    //element.removeClass("empty");
                }
                else {
                    imageStore.getOrInsert(value).then(function (content) {
                        if (content) {
                            //console.log("set src ",value," from image store");
                            attr.$set(name, content.image);
                            //picturefill({ reevaluate: true });
                            //element.removeClass("empty");
                        }
                        else { //sauver dans le cache ?
                            //console.log("set src ", value)
                            attr.$set(name, value);
                            //picturefill({ reevaluate: true });
                            //element.removeClass("empty");
                        }
                    }).catch(function(err){
                        //console.log("set src=", value, " error after read image store",err);
                        attr.$set(name, value);
                        //picturefill({ reevaluate: true });
                        //element.removeClass("empty");
                    });
                }
                //if (msie && propName) element.prop(propName, attr[name]);
            });
        }
    };
} ]);

app.directive("ngSrcsetCache", ["imageStore", function (imageStore) {

    return {
        priority: 99,
        link: function (scope, element, attr) {
            //element.addClass("preloadable");
            //element.addClass("empty");
            var propName = "srcset",
            name = "srcset";

            attr.$observe("ngSrcsetCache", function (value) {
                if (!value) return;
                if (value.match(dataUrl)) {
                    attr.$set(name, value);
                    //picturefill({reevaluate: true});
                    //element.removeClass("empty");
                }
                else {
                    imageStore.getOrInsert(value).then(function (content) {
                        if (content) {
                            attr.$set(name, content.image);
                            //picturefill({reevaluate: true});
                            //element.removeClass("empty");
                        }
                        else { //sauver dans le cache ?
                            attr.$set(name, value);
                            //picturefill({reevaluate: true});
                            //element.removeClass("empty");
                        }
                    }).catch(function(err){
                        attr.$set(name, value);
                        //picturefill({ reevaluate: true });
                        //element.removeClass("empty");
                    });
                }
                //if (msie && propName) element.prop(propName, attr[name]);
            });
        }
    };
} ]);